
#include "stdafx.h"
#include "WicBitmapDecoder.h"
#include "WicBitmapFrameDecode.h"

//==============================================================================
// CWicBitmapDecoder
//==============================================================================
CWicBitmapDecoder::CWicBitmapDecoder( 
    /* [in] */ __RPC__in LPCWSTR wzFilename,
    /* [unique][in] */ __RPC__in_opt const GUID *pguidVendor,
    /* [in] */ DWORD dwDesiredAccess,
    /* [in] */ WICDecodeOptions metadataOptions)
{
    m_pIWICBitmapDecoder = NULL;
    g_hrLast = g_theImagingFactory->CreateDecoderFromFilename(
        wzFilename, pguidVendor, dwDesiredAccess, metadataOptions, &m_pIWICBitmapDecoder);
}

CWicBitmapDecoder::CWicBitmapDecoder(
    /* [in] */ IStream *pIStream,
    /* [unique][in] */ const GUID *pguidVendor,
    /* [in] */ WICDecodeOptions metadataOptions)
{
    m_pIWICBitmapDecoder = NULL;
    g_hrLast = g_theImagingFactory->CreateDecoderFromStream(
        pIStream, pguidVendor, metadataOptions, &m_pIWICBitmapDecoder);
}

CWicBitmapDecoder::~CWicBitmapDecoder()
{
    if(m_pIWICBitmapDecoder)
    {
        m_pIWICBitmapDecoder->Release();
    }
    m_pIWICBitmapDecoder = NULL;
}

CWicBitmapDecoder::CWicBitmapDecoder(CWicBitmapDecoder const & objBitmapDecoder)
{
    m_pIWICBitmapDecoder = objBitmapDecoder.m_pIWICBitmapDecoder;
    m_pIWICBitmapDecoder->AddRef();
}

CWicBitmapDecoder& CWicBitmapDecoder::operator=(CWicBitmapDecoder const & objBitmapDecoder)
{
    if(m_pIWICBitmapDecoder)
    {
        m_pIWICBitmapDecoder->Release();
    }
    m_pIWICBitmapDecoder = objBitmapDecoder.m_pIWICBitmapDecoder;
    m_pIWICBitmapDecoder->AddRef();
    return *this;
}

//==============================================================================
// IWICBitmapDecoder
//==============================================================================
HRESULT CWicBitmapDecoder::QueryCapability( 
    /* [in] */ __RPC__in_opt IStream *pIStream,
    /* [out] */ __RPC__out DWORD *pdwCapability)
{
    return m_pIWICBitmapDecoder->QueryCapability(pIStream, pdwCapability);
}

HRESULT CWicBitmapDecoder::Initialize( 
    /* [in] */ __RPC__in_opt IStream *pIStream,
    /* [in] */ WICDecodeOptions cacheOptions)
{
    return m_pIWICBitmapDecoder->Initialize(pIStream, cacheOptions);
}

HRESULT CWicBitmapDecoder::GetContainerFormat( 
    /* [out] */ __RPC__out GUID *pguidContainerFormat)
{
    return m_pIWICBitmapDecoder->GetContainerFormat(pguidContainerFormat);
}

HRESULT CWicBitmapDecoder::GetDecoderInfo( 
    /* [out] */ __RPC__deref_out_opt IWICBitmapDecoderInfo **ppIDecoderInfo)
{
    return m_pIWICBitmapDecoder->GetDecoderInfo(ppIDecoderInfo);
}

HRESULT CWicBitmapDecoder::CopyPalette( 
    /* [in] */ __RPC__in_opt CWicPalette *pIPalette)
{
    return m_pIWICBitmapDecoder->CopyPalette(pIPalette->GetPtr());
}

HRESULT CWicBitmapDecoder::GetMetadataQueryReader( 
    /* [out] */ __RPC__deref_out_opt IWICMetadataQueryReader **ppIMetadataQueryReader)
{
    return m_pIWICBitmapDecoder->GetMetadataQueryReader(ppIMetadataQueryReader);
}

HRESULT CWicBitmapDecoder::GetPreview( 
    /* [out] */ __RPC__deref_out_opt IWICBitmapSource **ppIBitmapSource)
{
    return m_pIWICBitmapDecoder->GetPreview(ppIBitmapSource);
}

HRESULT CWicBitmapDecoder::GetColorContexts( 
    /* [in] */ UINT cCount,
    /* [size_is][unique][out][in] */ IWICColorContext **ppIColorContexts,
    /* [out] */ __RPC__out UINT *pcActualCount)
{
    return m_pIWICBitmapDecoder->GetColorContexts(cCount, ppIColorContexts, pcActualCount);
}

HRESULT CWicBitmapDecoder::GetThumbnail( 
    /* [out] */ __RPC__deref_out_opt IWICBitmapSource **ppIThumbnail)
{
    return m_pIWICBitmapDecoder->GetThumbnail(ppIThumbnail);
}

HRESULT CWicBitmapDecoder::GetFrameCount( 
    /* [out] */ __RPC__out UINT *pCount)
{
    return m_pIWICBitmapDecoder->GetFrameCount(pCount);
}

UINT CWicBitmapDecoder::GetFrameCount()
{
    UINT uCount = 0;
    m_pIWICBitmapDecoder->GetFrameCount(&uCount);
    return uCount;
}

HRESULT CWicBitmapDecoder::GetFrame( 
    /* [in] */ UINT index,
    /* [out] */ __RPC__deref_out_opt IWICBitmapFrameDecode **ppIBitmapFrame)
{
    return m_pIWICBitmapDecoder->GetFrame(index, ppIBitmapFrame);
}

CWicBitmapFrameDecode* CWicBitmapDecoder::GetFrame(UINT uIndex)
{
    return new CWicBitmapFrameDecode(this, uIndex);
}



